package com.wjc.server.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wjc.server.dto.${Domain}Dto;
import com.wjc.server.dto.PageDto;
import com.wjc.server.entity.${Domain};
import com.wjc.server.entity.${Domain}Example;
import com.wjc.server.mapper.${Domain}Mapper;
import com.wjc.server.util.CopyUtil;
import com.wjc.server.util.SnowFlake;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
<#list  typeSet as type>
    <#if type=='Date'>
       import java.util.Date;
    </#if>
</#list>


@Service
public class ${Domain}Service {
    @Resource
    private ${Domain}Mapper ${domain}Mapper;

    @Resource
    private SnowFlake snowFlake;

    //查询
    public void list(PageDto pageDto){
        PageHelper.startPage(pageDto.getPage(),pageDto.getSize());
        ${Domain}Example ${domain}Example = new ${Domain}Example();
        <#list  fieldList as field>
            <#if field.nameHump=='sort'>
                ${domain}Example.setOrderByClause("sort asc");
            </#if>
        </#list>
        List<${Domain}> ${domain}List= ${domain}Mapper.selectByExample(${domain}Example);
        PageInfo<${Domain}> pageInfo=new PageInfo<>(${domain}List);
        pageDto.setTotal(pageInfo.getTotal());

        List<${Domain}Dto> ${domain}DtosList = CopyUtil.copyList(${domain}List,${Domain}Dto.class);
        pageDto.setList(${domain}DtosList);

    }

    //保存
    public void save(${Domain}Dto ${domain}Dto){
    <#list  typeSet as type>
        <#if type=='Date'>
            Date now =new Date();
        </#if>
    </#list>

        ${Domain} ${domain}=CopyUtil.copy(${domain}Dto,${Domain}.class);
        if (ObjectUtils.isEmpty(${domain}.getId())){
            //新增
        <#list fieldList as field>
            <#if field.nameHump=='createdAt'>
                ${domain}.setCreatedAt(now);
            </#if>
            <#if field.nameHump=='updatedAt'>
                ${domain}.setUpdatedAt(now);
            </#if>
        </#list>
            ${domain}.setId(snowFlake.nextId());
            ${domain}Mapper.insert(${domain});
        }else {
             <#list fieldList as field>
                 <#if field.nameHump=='updatedAt'>
                     ${domain}.setUpdatedAt(now);
                 </#if>
             </#list>
            ${domain}Mapper.updateByPrimaryKey(${domain});
        }

    }

    public void delete(Long id){
        ${domain}Mapper.deleteByPrimaryKey(id);
    }
}
